package ee.fj.http;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class NetworkInterfaces {
	public static InetAddress[] getAvailableAddresses(boolean includelocal) throws SocketException {
		ArrayList<InetAddress> rv = new ArrayList<InetAddress>();
        for (NetworkInterface netint : Collections.list(NetworkInterface.getNetworkInterfaces())) {
	        List<InetAddress> l = Collections.list(netint.getInetAddresses());
			for (InetAddress inetAddress : l) {
				if (inetAddress.getAddress().length == 4 && (includelocal || inetAddress.getAddress()[0] != 127)) {
					rv.add(inetAddress);
				}
	        }
        }
        return rv.toArray(new InetAddress[rv.size()]);
	}
}
